#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>

#define true 1
#define false 0

/*bool Collision(int ennemi_x,int curseur_y,struct lucy box)
        {
           if (curseur_x >= box.x

            && curseur_x < box.x + box.w
            && curseur_y >= box.y
            && curseur_y < box.y + box.h)
               return true;
           else
               return false;
        }*/


int main(int argc, char *argv[])
{
    SDL_Surface *ecran = NULL, *lucy = NULL , *imagedefond = NULL, *ennemi=NULL;
    SDL_Rect posfond , positionlucy, posennemi;
    int continuer = 1, d =0, g=1;
    int i = 2200,k=0 ;

    ecran = SDL_SetVideoMode(900, 600, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);
    SDL_WM_SetCaption("déplacement", NULL);

    imagedefond = SDL_LoadBMP("tunis.bmp");

    lucy = SDL_LoadBMP("a.bmp");
     ennemi= SDL_LoadBMP("enmi1.bmp"); 
 
 posennemi.x =700;
 posennemi.y = 420;


    positionlucy.x = 0 ;
    positionlucy.y = 600;


    SDL_Rect posMarioAbs;
    posMarioAbs.x = 0;
    posMarioAbs.y = 600-(lucy->h);

  // La position relative de Mario
    SDL_Rect posMarioRel;
    posMarioRel.x = -80;
    posMarioRel.y = 600;

    posfond.x = 0 ;
    posfond.y= 0 ;


    SDL_Event event;

    SDL_Init(SDL_INIT_VIDEO);

  

if(posennemi.x==0)
posennemi.x++;        
    SDL_EnableKeyRepeat(7,7);
    while (continuer)
    { //posennemi.x--;
        SDL_WaitEvent(&event);
        switch(event.type)
        {
        case SDL_QUIT:
            continuer = 0;
            break;
        case SDL_KEYDOWN:
            switch(event.key.keysym.sym)
            {

            case SDLK_RIGHT:
          posMarioAbs.x++;
                i++ ;

                if (i%7==0)
                    lucy= SDL_LoadBMP("a.bmp");//ne jamais utiliser un chargement en mémoire dans une game loop

                if (i%7==1)
                    lucy= SDL_LoadBMP("a.bmp");
 ennemi= SDL_LoadBMP("enmi1.bmp"); 
                if (i%7==2)
                    lucy= SDL_LoadBMP("b.bmp");
 
                if (i%7==3)
                    lucy= SDL_LoadBMP("b.bmp");

                if (i%7==4)
                    lucy= SDL_LoadBMP("c.bmp");

                if (i%7==5)
                    lucy= SDL_LoadBMP("c.bmp");
 
                if (i%7==6)
                    lucy= SDL_LoadBMP("c.bmp");

                if (posMarioAbs.x == 700)
                {
        posMarioAbs.x =0;
        posMarioAbs.y =318;
                    if(d==0)
                        imagedefond = SDL_LoadBMP("fond3.bmp");
                    if(d==1)
                        imagedefond = SDL_LoadBMP("fond1.bmp");
                    if(d==2)
                        imagedefond = SDL_LoadBMP("fond2.bmp");
                    
                    d++;
                }

                break;

            case SDLK_LEFT:
posMarioAbs.x--;
                i-- ;
                if (i%7==0)
                    lucy= SDL_LoadBMP("111.bmp");

                if (i%7==1)
                    lucy= SDL_LoadBMP("111.bmp");

                if (i%7==2)
                    lucy= SDL_LoadBMP("222.bmp");

                if (i%7==3)
                    lucy= SDL_LoadBMP("222.bmp");

                if (i%7==4)
                    lucy= SDL_LoadBMP("333.bmp");


                if (i%7==5)
                    lucy= SDL_LoadBMP("333.bmp");

                if (i%7==6)
                    lucy= SDL_LoadBMP("333.bmp");


                break;
  case SDLK_UP:

         posMarioRel.x+=15;
    
         if(posMarioRel.x>=80)
          { 
           posMarioRel.x = 80;
}
       
          //On met à "0" les pos abs:

          posMarioAbs.x = 1;
          posMarioAbs.y = 0 -(lucy->h);

            //On calcule la valeur relative de y:
 
            posMarioRel.y=(-0.1*(posMarioRel.x*posMarioRel.x)+80);
            //On calcule maintenant les valeurs abs
 
            posMarioAbs.x = posMarioAbs.x + posMarioRel.x + 100;
            posMarioAbs.y = posMarioAbs.y - posMarioRel.y;

        //Intervalle de 1ms
       SDL_Delay(1);

       break;
            }
            break;

        }
         posennemi.x--;

if (posennemi.x ==posMarioAbs.x)
{
ennemi= SDL_LoadBMP("a.bmp"); 
}

        SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
        SDL_BlitSurface(imagedefond, NULL, ecran, &posfond);

        SDL_SetColorKey(lucy, SDL_SRCCOLORKEY, SDL_MapRGB(lucy->format, 47, 48,130));
            SDL_BlitSurface(lucy, 0, ecran, &posMarioAbs);

SDL_SetColorKey(ennemi, SDL_SRCCOLORKEY, SDL_MapRGB(ennemi->format, 51, 51,255));
     SDL_BlitSurface(ennemi, 0, ecran, &posennemi);
     
       // SDL_BlitSurface(lucy, NULL, ecran, &positionlucy);


        SDL_Flip(ecran);

    SDL_FreeSurface(lucy);
 SDL_FreeSurface(ennemi);

    SDL_FreeSurface(imagedefond);
    SDL_Quit();
    return EXIT_SUCCESS;
}

